package com.woniuxy.sales.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.sales.common.enumeration.BusinessCode;
import com.woniuxy.sales.model.entity.Products;
import com.woniuxy.sales.service.ProductsService;
import com.woniuxy.sales.untils.Assert;
import com.woniuxy.sales.untils.JwtTemplate;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Info;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/car")
@Slf4j
public class ProductsController {

    @Autowired
    private ProductsService productsService;
    @Autowired
    private JwtTemplate jwtTemplate;

    @PostMapping("/all")
    @ApiOperation("查询全部")
    public ResponseEntity<Page<Products>> all(@RequestBody Map<String, Integer> params) {
        //获取页码和页面大小
        int pageNum = params.get("pageNum");
        int pageSize = params.get("pageSize");
        //调用业务层查询
        Page<Products> page = productsService.findAll(pageNum, pageSize);
        return ResponseEntity.ok(page);
    }

    @PostMapping("/findByType")
    @ApiOperation("按类型显示")
    public ResponseEntity<Page<Products>> findByType(@RequestBody Map<String, Integer> params) {
        //获取页码和页面大小
        int pageNum = params.get("pageNum");
        int pageSize = params.get("pageSize");
        //获取分类类型
        int typeId = params.get("typeId");
        //调用业务层查询
        Page<Products> page = productsService.findByType(pageNum, pageSize, typeId);
        return ResponseEntity.ok(page);
    }

    @PostMapping("/findByPrice")
    @ApiOperation("按价格显示")
    public ResponseEntity<Page<Products>> findByPrice(@RequestBody Map<String, Object> params) {
        // 获取页码和页面大小
        int pageNum = (int) params.get("pageNum");
        int pageSize = (int) params.get("pageSize");

        // 获取价格范围
        Integer minPrice = (Integer) params.get("minPrice");
        Integer maxPrice = (Integer) params.get("maxPrice");

        // 调用业务层查询
        Page<Products> page = productsService.findByPrice(pageNum, pageSize, minPrice, maxPrice);
        return ResponseEntity.ok(page);
    }

    @PostMapping("/{productId}")
    @ApiOperation("添加商品到对比列表")
    public ResponseEntity<?> add(@RequestHeader(name = "Authorization", required = false) String token,
            @PathVariable("productId") Long id) {
        try {
            Assert.error(! jwtTemplate.validateToken(token), BusinessCode.INVALID_TOKEN);
            String userId = jwtTemplate.getValue(token, "userId");
            Products car = productsService.addCar(userId,id);
            return ResponseEntity.ok(car);
        }catch (RuntimeException e){
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }


    @GetMapping("/getCompare")
    @ApiOperation("查询对比")
    public ResponseEntity<List<Products>> getCompare(@RequestHeader(name = "Authorization", required = false) String token) {
        Assert.error(! jwtTemplate.validateToken(token), BusinessCode.INVALID_TOKEN);
        String userId = jwtTemplate.getValue(token, "userId");
        List<Products> compare = productsService.getCompare(userId);
        return ResponseEntity.ok(compare);

    }

    @PostMapping("/removeCompare")
    @ApiOperation("删除对比")
    public ResponseEntity<List<Products>> removeCompare(@RequestHeader(name = "Authorization", required = false) String token,
            @RequestBody Map<String, String> request) {
        Assert.error(! jwtTemplate.validateToken(token), BusinessCode.INVALID_TOKEN);
        String userId = jwtTemplate.getValue(token, "userId");
        String productId = request.get("productId");
        List<Products> compare = productsService.removeCompare(userId,productId);
        return ResponseEntity.ok(compare);
    }

    @PostMapping("/checkCompare/{productId}")
    @ApiOperation("判断是否已经对比")
    public ResponseEntity<Boolean> checkAddedToCompare(@PathVariable String productId) {
        Boolean exists = productsService.checkCompare(productId);
        return ResponseEntity.ok(exists);
    }


    @GetMapping("/getCarById")
    @ApiOperation("根据id查询")
    public ResponseEntity<Products> getCarById(@RequestParam("id") Integer id) {
        Products car = productsService.findById(id);
        return ResponseEntity.ok(car);
    }




}
